 <!DOCTYPE html>
 <html lang="en">

 <head>
     <meta charset="UTF-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>计算BMI</title>
 </head>

 <body>
     <h1>BMI测试</h1>
     <form action="" method="post">
         <label for="realname">姓名：</label>
         <input type="text" name="realname" id="realname" placeholder="请输入姓名" required>
         <br>
         <label for="height">身高(cm)：</label>
         <input type="text" name="height" id="height" placeholder="请输入身高" required>
         <br>
         <label for="weight">体重(kg)：</label>
         <input type="text" name="weight" id="weight" placeholder="请输入体重" required>
         <br>
         <input type="submit" name="submit" value="计算BMI">
     </form>
 </body>

 </html>

 <?php
    if (isset($_POST['submit'])) { // 如果提交按钮被点击
        $realname = $_POST['realname']; // 获取身高
        $height = $_POST['height']; // 获取身高
        $weight = $_POST['weight']; // 获取体重
        $result = calculateBMI($height / 100, $weight);

        echo $realname . "的BMI测试结果<br/>";
        echo "身高：" . $height . "cm<br>"; // 输出身高
        echo "体重：" . $weight . "kg<br>"; // 输出体重
        echo "BMI指数：" . $result . "<br>"; // 输出BMI指数

    }

    function calculateBMI($height, $weight)
    {
        $bmi = round($weight / ($height * $height), 2); // 计算BMI指数，保留1位小数

        if ($bmi < 18.5) { // 如果BMI指数小于18.5
            return $bmi . " ，消瘦";
        } elseif ($bmi >= 18.5 && $bmi < 24.9) { // 如果BMI指数在18.5到24.9之间
            return $bmi . " ，正常";
        } elseif ($bmi >= 24.9 && $bmi < 28) { // 如果BMI指数在24.9到27.9之间
            return $bmi . " ，超重";
        } else { // 如果BMI指数大于等于28
            return $bmi . " ，肥胖";
        }
    }

    ?>